﻿/*
' Copyright (c) 2011  DotNetNuke Corporation
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
*/

using System;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Security;
using DotNetNuke.Security.Roles;

using System.Web.UI.WebControls;
using System.Data.SqlClient;
using DotNetNuke.Framework.Providers;
using DotNetNuke.Modules.PersonalAssistants.Components;

namespace DotNetNuke.Modules.PersonalAssistants
{

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The ViewPersonalAssistants class displays the content
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class CreatePAProfile : PersonalAssistantsModuleBase, IActionable
    {

        #region Event Handlers

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }


        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Page_Load runs when the control is loaded
        /// </summary>
        /// -----------------------------------------------------------------------------
        private const string providerType = "data";
        private ProviderConfiguration providerConfiguration = ProviderConfiguration.GetProviderConfiguration(providerType);
        private string connectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {

                    Provider provider = (Provider)providerConfiguration.Providers[providerConfiguration.DefaultProvider];
                    connectionString = DotNetNuke.Common.Utilities.Config.GetConnectionString();

                    SqlConnection commonConnection = new SqlConnection();
                    commonConnection.ConnectionString = connectionString;
                    SqlCommand sharedSqlCommand;
                    SqlDataReader sharedSqlDataReader;

                    // Set up inputs
                    //ipUsername.Text = DateTime.Now.ToString();
                    if (!UserId.Equals(-1))
                    {
                        //(for the pre-amble)
                        lblUsername.Text = UserInfo.Username.ToString();

                        lblEmail.Text = UserInfo.Email.ToString();                        
                        
                        //(1)
                        ipUsername.Text = UserInfo.Username.ToString();
                        //(2)
                        ipEmail.Text = UserInfo.Email.ToString();
                    }

                    sharedSqlCommand = new SqlCommand("SELECT Name FROM SYCIL_LISTS WHERE ListName = 'LOCATIONS' ORDER BY ListOrder", commonConnection);
                    commonConnection.Open();
                    sharedSqlDataReader = sharedSqlCommand.ExecuteReader();
                    //(3)
                    ipPALocation.DataSource = sharedSqlDataReader;
                    ipPALocation.DataBind();
                    commonConnection.Close();
                    sharedSqlDataReader.Close();

                    //(4)
                    sharedSqlCommand = new SqlCommand("SELECT Name FROM SYCIL_LISTS WHERE ListName = 'LOCATIONS' ORDER BY ListOrder", commonConnection);
                    commonConnection.Open();
                    sharedSqlDataReader = sharedSqlCommand.ExecuteReader();
                    ipPALocations.DataSource = sharedSqlDataReader;
                    ipPALocations.DataBind();
                    commonConnection.Close();
                    sharedSqlDataReader.Close();

                    //(5)
                    sharedSqlCommand = new SqlCommand("SELECT Name FROM SYCIL_LISTS WHERE ListName = 'TASKS' ORDER BY ListOrder", commonConnection);
                    commonConnection.Open();
                    sharedSqlDataReader = sharedSqlCommand.ExecuteReader();
                    ipTaskList.DataSource = sharedSqlDataReader;
                    ipTaskList.DataBind();
                    commonConnection.Close();
                    sharedSqlDataReader.Close();

                    //(7)
                    sharedSqlCommand = new SqlCommand("SELECT Name FROM SYCIL_LISTS WHERE ListName = 'AVAILABLE' ORDER BY ListOrder", commonConnection);
                    commonConnection.Open();
                    sharedSqlDataReader = sharedSqlCommand.ExecuteReader();
                    ipHoursAvailable.DataSource = sharedSqlDataReader;
                    ipHoursAvailable.DataBind();
                    commonConnection.Close();
                    sharedSqlDataReader.Close();

                    //(8)
                    sharedSqlCommand = new SqlCommand("SELECT Name FROM SYCIL_LISTS WHERE ListName = 'DAYS' ORDER BY ListOrder", commonConnection);
                    commonConnection.Open();
                    sharedSqlDataReader = sharedSqlCommand.ExecuteReader();
                    ipDaysAvailable.DataSource = sharedSqlDataReader;
                    ipDaysAvailable.DataBind();
                    commonConnection.Close();
                    sharedSqlDataReader.Close();

                    

                }
                catch (Exception exc) //Module failed to load
                {
                    Exceptions.ProcessModuleLoadException(this, exc);
                }//end try catch
            }//end postback
        }//end page load
        protected void submittedProfile(object sender, EventArgs e)
        {
            PersonalAssistantProfile oProfile = new PersonalAssistantProfile();
            oProfile.ALittleBitAboutMyself = ipALittleBitAboutPANotes.Text.ToString();
            oProfile.BarredByISA = ipBarredByISA.SelectedValue.ToString();
            oProfile.CarOwner = ipCarOwner.SelectedValue.ToString();
            oProfile.DateOfPAUpdate = DateTime.Now;
            oProfile.DateTime = DateTime.Now;
                // Days available
                foreach (ListItem lstItm in ipDaysAvailable.Items)
                {
                    if (lstItm.Selected)
                        oProfile.DaysAvailable += (lstItm.Text.ToString() + ",");
                }
                if (oProfile.DaysAvailable.EndsWith(","))
                    oProfile.DaysAvailable = oProfile.DaysAvailable.TrimEnd(',');

            oProfile.DaysPerWeek = ipDaysRequired.Text.ToString();
            oProfile.DrivingLicenceHeld = ipDrivingLicence.SelectedValue.ToString();
            oProfile.ECRWilling = ipECRcheck.SelectedValue.ToString();
            oProfile.Email = ipEmail.Text.ToString();
            oProfile.EmergencyCallout = ipEmergencyCallout.SelectedValue.ToString();
            oProfile.HoursPerDay = ipHoursRequired.Text.ToString();
            oProfile.IsPersonalAssistant = "Yes";
            oProfile.Languages = ipOtherLanguages.SelectedValue.ToString();
            oProfile.LateNights = ipLateNights.SelectedValue.ToString();
                // Locations
                foreach(ListItem lstItm in ipPALocations.Items){
                    if (lstItm.Selected) 
                        oProfile.Locations += (lstItm.Text.ToString() + ",");
                }
	            if (oProfile.Locations.EndsWith(","))
                    oProfile.Locations = oProfile.Locations.TrimEnd(',');
            oProfile.MaleOrFemale = ipMaleOrFemale.SelectedValue.ToString();
            oProfile.MeatProducts = ipMeatProducts.SelectedValue.ToString();
            oProfile.ModuleId = this.ModuleId;
            oProfile.OvernightWakingNights = ipOvernightWakingNights.SelectedValue.ToString();
            oProfile.PALocation = ipPALocation.Text.ToString();
            oProfile.PermissionForUK = ipPermissionForUK.SelectedValue.ToString();
            oProfile.ProfileApproved = ipProfileApproved.SelectedValue.ToString();
            oProfile.Provide2References = ipReferencesAvailable.SelectedValue.ToString();
            oProfile.PublishThisProfile = ipPublishProfile.SelectedValue.ToString();
            oProfile.ReferenceNotes = ipReferenceNotes.Text.ToString();
            oProfile.SchoolHolidays = ipSchoolHolidays.SelectedValue.ToString();
            oProfile.ServiceRequired = "";
            oProfile.SkillNotes = ipQualificationsAndTrainingNotes.Text.ToString();
            oProfile.Smoker = ipSmoker.SelectedValue.ToString();
            oProfile.TaskNotes = ipTaskNotes.Text.ToString();
                // Tasks
                foreach (ListItem lstItm in ipTaskList.Items)
                {
                    if(lstItm.Selected)
                      oProfile.Tasks += (lstItm.Text.ToString() + ",");
                }
                if (oProfile.Tasks.EndsWith(","))
                    oProfile.Tasks = oProfile.Tasks.TrimEnd(',');
            oProfile.TermTime = ipTermTime.SelectedValue.ToString();
                //Hours available
                foreach (ListItem lstItm in ipHoursAvailable.Items)
                    if (lstItm.Selected)
                        oProfile.TimesAvailable += lstItm.Text.ToString() + ",";
                if (oProfile.TimesAvailable.EndsWith(","))
                    oProfile.TimesAvailable = oProfile.TimesAvailable.TrimEnd(',');
            oProfile.Username = ipUsername.Text.ToString();
            oProfile.WorkpatternNotes = ipWorkPatternNotes.Text.ToString();
            oProfile.YearsResident = ipAddressYears.Text.ToString();            
            
            FeatureController oController = new FeatureController();
            oController.InsertPAProfile(oProfile);
        }

        #endregion

        #region Optional Interfaces

        public ModuleActionCollection ModuleActions
        {
            get
            {
                ModuleActionCollection Actions = new ModuleActionCollection();
                Actions.Add(GetNextActionID(), Localization.GetString("EditModule", this.LocalResourceFile), "", "", "", EditUrl(), false, SecurityAccessLevel.Edit, true, false);
                return Actions;
            }
        }

        #endregion

    }

}
